#include <msp430x26x.h>
#include "UART_HANDLER.h"
#include <stdio.h>


extern struct UART_RX_BUF S_UART_BUFFER;
char tempString[10];
char *tempStringPtr;
char helloString[5] = {'h','e','l','l','o'};

/**************************************************
tx_byte_to_slave - fn sends a single byte using UART0
**************************************************/
void tx_byte_to_slave(unsigned char Byte)
{
		while (!(UC1IFG & UCA1TXIFG));	// USART1 TX buffer ready?
  	UCA1TXBUF = Byte;                		// RXBUF1 to TXBUF1
}
/*************************************************/

/**************************************************
tx_slave_string - function transmits a null terminated string pointed to by *Tx_string
**************************************************/
void tx_slave_string(char *Tx_string)
{

	while((*Tx_string) != '\0')
	{
		while (!(UC1IFG & UCA1TXIFG));  // USART0 TX buffer ready?
		UCA1TXBUF = *Tx_string++;		// push byte out, increment pointer
	}
}
/*************************************************/

/**************************************************
S_parseRXCmd - fn reads in serial commands and responds to them
**************************************************/
void S_parseRXCmd(void)
{
	int chars;

	if (S_UART_BUFFER.Buffer[0] == '1')
	{
		tx_byte_to_slave('1');
		if(chars = sprintf(tempStringPtr, "%d", 12) > 0) //uses sprintf to get the string version of a number and the number of characters in the string
		{
			tx_byte_to_slave(0x0D);
  			tx_byte_to_slave(0x0A);
  			tx_byte_to_slave(0x0D);
  			tx_byte_to_slave(0x0A);
  			tx_slave_string(helloString);
			tx_slave_string("Current ID: ");
			tempString[chars + 1] = 0x00;			// null terminated.
			tx_slave_string(tempStringPtr);
		}
		else {tx_byte_to_slave(0x0D); tx_byte_to_slave(0x0A);tx_slave_string("Error finding ID");}
		S_UART_BUFFER.Buffer_Length = 0;
	}

}


